package com.module.msg.model

import android.text.TextUtils
import cn.leancloud.LCObject
import cn.leancloud.LCQuery
import com.module.app.base.BaseViewModel
import com.module.app.bean.ListViewBean
import com.module.app.utils.UserUtils
import com.module.base.ext.request
import com.module.base.utils.AppUtils
import com.module.frame.ext.postEvent
import com.module.msg.bean.MsgBean
import com.module.third.learcloud.lcQuery
import com.module.third.learcloud.LCResponse
import com.module.third.learcloud.SUCCESS_CODE

class MyMsgViewModel : BaseViewModel() {
    fun getData() {
        request({
            var userId: String? = com.module.app.utils.UserUtils.getUserId()
            if (userId.isNullOrEmpty()) {
                userId = AppUtils.getAndroidId()
            }
            if (TextUtils.isEmpty(userId)) {
                return@request LCResponse<MutableList<MsgBean>>(SUCCESS_CODE, "", mutableListOf())
            }
            val tClass = MsgBean::class.java
            val query = LCQuery<LCObject>(tClass.simpleName)
            query.whereEqualTo("userId", userId)
            return@request lcQuery(query, tClass)
        }, {
            postEvent(listFlow, ListViewBean(true, it, true))
        }, {
            postEvent(errorFlow, it)
        })
    }
}